<?php
/**
 *
 *
 */
namespace Yz\modules\Generic;

class Controller_Create
    extends \Yz_Controller_Templated
    implements \Yz_Controller_Interface
{

    /**
     *  forbid access beyond admin panel
     *
     */
    const ACCESS = \Yz_Access_Interface::AC_ADMIN;

    /**
     *  @var string
     */
    protected $modelName = "Default";

    /**
     *
     */
    protected function doGET()
    {
        debug();
        $model = $this->module->selectModel($this->modelName);
        $item = $model->selectItem();

        $form = new \Yz_Form_Default($this->module);
        $form
            ->assignElements($item->describe())
            ->assignValues($item)
            ->setName("theForm");

        $template = $this->selectTemplate();
        $template->item = $item;
        $template->includeForm($form);

        return $this->outputTemplate($template);
    }

    /**
     *
     */
    protected function doPOST()
    {
        debug();
        $model = $this->module->selectModel($this->modelName);
        $item = $model->selectItem();

        $form = new \Yz_Form_Default($this->module);
        $form
            ->setName("theForm")
            ->assignElements($item->describe());
        try {
            $form->parse($this->getRequest()->getPOSTArguments());
            $item->assignValues($form->getValues());
            $model->addItem($item);
            $ex = false;
            $status = '#' . $this->classNameUnqualified() . ':Success';
        } catch (\Yz_Form_Parser_Exception $ex) {
            debug($ex->toString(true));
            $status = false;
            // TODO add global error message
        }

        $template = $this->selectTemplate();
        $template->item = $item;
        $template->includeForm($form);

        $template->YzErrorMessage = $ex;
        $template->YzSuccess = $status;

        return $this->outputTemplate($template);
    }


}
